<template>
    <div>
        <!-- 面包屑区域 -->
        <el-breadcrumb separator-class="el-icon-arrow-right">
            <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>用户管理</el-breadcrumb-item>
            <el-breadcrumb-item>用户列表</el-breadcrumb-item>
        </el-breadcrumb>

        <!-- 卡片主体内容 -->
        <el-card class="box-card">
            <!-- 搜索区域 -->
            <el-row :gutter="20">
                <el-col :span="8">
                    <el-input v-model="queryInfo.query" placeholder="请输入内容" clearable @keyup.enter.native="searchGoods"
                        @clear="searchGoods">
                        <el-button @click="searchGoods" slot="append" icon="el-icon-search"></el-button>
                    </el-input>
                </el-col>
                <el-col :span="4">
                    <el-button type="primary" @click="() => {this.$router.push('/goods/add')}">添加商品</el-button>
                </el-col>
            </el-row>

            <!-- 商品列表 -->
            <el-table :data="goodsList" border stripe>
                <el-table-column type="index">
                </el-table-column>
                <el-table-column prop="goods_name" label="商品名称">
                </el-table-column>
                <el-table-column prop="goods_price" width="95px" label="商品价格（元）">
                </el-table-column>
                <el-table-column prop="goods_weight" width="70px" label="重量">
                </el-table-column>
                <el-table-column prop="add_time" width="140px" label="创建时间">
                    <template slot-scope="scope">
                        {{ scope.row.add_time | dateFmt() }}
                    </template>
                </el-table-column>

                <el-table-column width="130px" label="操作">
                    <template slot-scope="scope">
                        <!-- 修改按钮 -->
                        <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
                        <!-- 删除按钮 -->
                        <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!-- 分页器 -->
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="queryInfo.pagenum" :page-sizes="[10, 20, 50, 100]" :page-size="queryInfo.pagesize"
                layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-card>

    </div>
</template>
<script>
export default {
    data() {
        return {
            goodsList: [],
            queryInfo: {
                query: '',
                pagesize: 10,
                pagenum: 1,
            },
            total: 0,
        }
    },
    created() {
        this.getGoodsList()
    },
    methods: {
        async getGoodsList() {
            const res = await this.$http.get('goods', { params: this.queryInfo })
            if (res.status == 200) {
                this.goodsList = res.data.data.goods
                this.total = res.data.data.total
            }
        },

        searchGoods() {
            this.pagenum = 1
            this.getGoodsList()
        },

        handleSizeChange(newSize) {
            this.queryInfo.pagesize = newSize;
            this.getGoodsList();
        },
        handleCurrentChange(newPage) {
            this.queryInfo.pagenum = newPage;
            this.getGoodsList();
        },
    }
}
</script>
<style scoped></style>